package com.javaweb.dao.impl;

import com.javaweb.common.StringUtils;
import com.javaweb.dao.PermDao;
import com.javaweb.model.Permission;
import com.javaweb.model.Role;
import com.javaweb.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/24 22:43
 */

/**
 * todo: 实现全部如下方法
 */
@Repository
@Slf4j
public class PermDaoImpl implements PermDao {

    @Autowired
    private Connection conn ;

    @Override
    public Integer insert(Permission perm) {
        int res = 0;
        String sql = "INSERT INTO tb_permission (name,url) VALUES"+
                "(?,?)" ;
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ) {
            ps.setString(1, perm.getName()) ;
            ps.setString(2,perm.getUrl());
            res = ps.executeUpdate() ;
        } catch (SQLException e) {
            log.error("execute insert user error: " + e.getMessage()) ;
        }
        return res ;
    }

    @Override
    public Integer insertRolePerm(int roleId, int permId) {
        int res = 0;
        String sql = "INSERT INTO tb_role_per (role_id,per_id) value (?,?)";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ) {
            ps.setInt(1, roleId);
            ps.setInt(2, roleId);
            res = ps.executeUpdate() ;
        } catch (SQLException e) {
            log.error("execute insert user error: " + e.getMessage()) ;
        }
        return res;
    }

    @Override
    public Permission selectOne(String name, String url) {
        String sql = "SELECT * FROM tb_permission WHERE name = ? and url = ?" ;
        try (PreparedStatement ps = this.conn.prepareStatement(sql) ;
        ) {
            ps.setString(1, name) ;
            ps.setString(2, url) ;
            ResultSet rs = ps.executeQuery() ;
            if (rs.next()) {
                Permission permission = new Permission()
                        .setPerId(rs.getInt(1))
                        .setName(name)
                        .setUrl(url);
                return permission ;
            }
        } catch (SQLException e) {
            log.error(e.getMessage()) ;
        }
        return null;
    }

    @Override
    public Permission selectOneById(int permId) {
        String sql = "SELECT * FROM tb_permission WHERE per_id = ?" ;
        try (PreparedStatement ps = this.conn.prepareStatement(sql) ;
        ) {
            ps.setInt(1, permId);
            ResultSet rs = ps.executeQuery() ;
            if (rs.next()) {
                Permission permission = new Permission()
                        .setPerId(rs.getInt(1))
                        .setName(rs.getString(2))
                        .setUrl(rs.getString(3));
                return permission ;
            }
        } catch (SQLException e) {
            log.error(e.getMessage()) ;
        }
        return null;
    }

    @Override
    public List<Permission> selectPermsByRoleName(String roleName) {
        List<Permission> permissions = new ArrayList<>();
        String sql = "select t1.per_id,t1.name,t1,url\n" +
                "from tb_permission t1\n" +
                "left join tb_role_per t2 on t1.per_id = t2.per_id\n" +
                "LEFT JOIN tb_role t3 on t2.role_id = t3.role_id\n" +
                "where t3.name = ?";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ) {
            ps.setString(1, roleName);
            ResultSet res = ps.executeQuery() ;
            while (res.next()){
                Permission permission = new Permission();
                permission.setPerId(res.getInt(1));
                permission.setName(res.getString(2));
                permission.setUrl(res.getString(3));
                permissions.add(permission);
            }
        } catch (SQLException e) {
            log.error("execute insert user error: " + e.getMessage()) ;
        }
        return permissions;
    }

    @Override
    public List<Permission> selectPermsByRoleId(int roleId) {
        List<Permission> permissions = new ArrayList<>();
        String sql = "select t1.per_id,t1.name,t1,url\n" +
                "from tb_permission t1\n" +
                "left join tb_role_per t2 on t1.per_id = t2.per_id\n" +
                "LEFT JOIN tb_role t3 on t2.role_id = t3.role_id\n" +
                "where t2.role_id = ?";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ) {
            ps.setInt(1, roleId);
            ResultSet res = ps.executeQuery() ;
            while (res.next()){
                Permission permission = new Permission();
                permission.setPerId(res.getInt(1));
                permission.setName(res.getString(2));
                permission.setUrl(res.getString(3));
                permissions.add(permission);
            }
        } catch (SQLException e) {
            log.error("execute insert user error: " + e.getMessage()) ;
        }
        return permissions;
    }

    @Override
    public Integer deleteById(int permId) {
        int res = 0;
        String sql = "DELETE FROM tb_permission WHERE per_id = ?";
        try(
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ){
            ps.setInt(1,permId);
            res = ps.executeUpdate();
        }catch (SQLException e){
            log.error("execute insert user error: " + e.getMessage()) ;
        }
        return res;
    }
}
